package chips440.internals;

import chips440.main.Cell;
import chips440.main.Types;
import java.util.Map;
import java.util.EnumMap;
import java.util.Map.Entry;

/**
 * This class holds the state modeling a player, including his position, the
 * items he has, and his uniquely assigned player id.
 *
 * @author Ian Voysey
 * @version 0.1
 */
public class Player{
    private Cell position;
    private Map<Types.Item, Boolean> items;
    private int my_id;

    /**
     * Create a new player.
     *
     * @param init_position where this player starts
     * @param id unique id handed down from Engine.init
     */
    public Player(Cell init_position, int id){
	items = new EnumMap<Types.Item, Boolean>(Types.Item.class);
	clearItems();

	this.position = init_position;
	my_id = id;
    }

    /**
     * Returns if a player hold a particular item.
     *
     * @param whichItem a constant specifying the item of interest, defined in
     * Item
     * @return true iff player has item whichItem
     */
    public boolean hasItem(Types.Item whichItem){
	Boolean ret_val = items.get(whichItem);
	if(ret_val == null)
	    return false;
	else
	    return ret_val.booleanValue();
    }
    /**
     * Allows the player to recieve an item, either from the board or
     * another player.
     *
     * @param whichItem the enum representing the item, as defined in
     * Types
     */
    public void giveItem(Types.Item whichItem){
	items.put(whichItem, Boolean.TRUE);
    }

    /**
     * Takes an item away from the player. This allows for single use keys,
     * and item trading between players as moderated by the Engine.
     *
     * @param whichItem the enum representing the item, defined in Types
     */
    public void takeItem(Types.Item whichItem){
	items.put(whichItem, Boolean.FALSE);
    }

    /**
     * returns this players ID
     *
     * @return the unique id for this player
     */
    public int getID(){
	return my_id;
    }

    /**
     * fetches this player's positon
     *
     * @return a cell where this player is located
     */
    public Cell getPosition(){
	return position;
    }

    /**
     * changes the players position on the board
     *
     * @param x the x coordinate
     * @param y the y coordinate
     */
    public void changePosition(int x, int y){
        this.position = new Cell(x, y);
    }

    /**
     * Clears this players item list, for use in level switching
     */
    public void clearItems(){
	for (Map.Entry<Types.Item, Boolean> entry : items.entrySet()) {
	    takeItem(entry.getKey());
	}
    }

    /**
     * A meaningful printing of the player object, useful for loading
     * images. Possibly a security leak.
     */
    public String toString(){
	return "" + my_id;
    }
}
